﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class ReplenishmentDA : IReplenishmentDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string FINAL_TO_LOC = "@FINAL_TO_LOC";
        private string FROM_LOC = "@FROM_LOC";
        private string ID_MOVED = "@ID_MOVED";
        private string LOC_MOVED_FROM = "@LOC_MOVED_FROM";
        private string LOT = "@LOT";
        private string LOT_MOVED = "@LOT_MOVED";
        private string LPN_ID = "@LPN_ID";
        private string OWNER_ID = "@OWNER_ID";
        private string PACK_ID = "@PACK_ID";
        private string PRIORITY = "@PRIORITY";
        private string QTY = "@QTY";
        private string QTY_INPICK_LOC = "@QTY_INPICK_LOC";
        private string QTY_MOVED = "@QTY_MOVED";
        private string REASON = "@REASON";
        private string REPLENISHMENT_GROUP = "@REPLENISHMENT_GROUP";
        private string REPLENISHMENT_ID = "@REPLENISHMENT_ID";
        private string SKU_ID = "@SKU_ID";
        private string SQL_CHECK_REPLENISHMENT_ID_UNIQUE = " SELECT COUNT(1) FROM @_@REPLENISHMENT WHERE REPLENISHMENT_ID = @REPLENISHMENT_ID AND REPLENISHMENT_GROUP = @REPLENISHMENT_GROUP ";
        private string SQL_DELETE_REPLENISHMENT = " DELETE FROM @_@REPLENISHMENT WHERE REPLENISHMENT_ID = @REPLENISHMENT_ID AND REPLENISHMENT_GROUP = @REPLENISHMENT_GROUP ";
        private string SQL_INSERT_REPLENISHMENT = " INSERT INTO @_@REPLENISHMENT ( WH_ID, REPLENISHMENT_ID, REPLENISHMENT_GROUP, OWNER_ID, SKU_ID, FROM_LOC, TO_LOC, LOT, LPN_ID, QTY, QTY_MOVED, QTY_INPICK_LOC, PRIORITY, UOM_ID, PACK_ID, ID_MOVED, LOT_MOVED, LOC_MOVED_FROM, REASON, TO_LPN_ID, FINAL_TO_LOC, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @REPLENISHMENT_ID, @REPLENISHMENT_GROUP, @OWNER_ID, @SKU_ID, @FROM_LOC, @TO_LOC, @LOT, @LPN_ID, @QTY, @QTY_MOVED, @QTY_INPICK_LOC, @PRIORITY, @UOM_ID, @PACK_ID, @ID_MOVED, @LOT_MOVED, @LOC_MOVED_FROM, @REASON, @TO_LPN_ID, @FINAL_TO_LOC, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_REPLENISHMENT = " SELECT  ROWID, WH_ID, REPLENISHMENT_ID, REPLENISHMENT_GROUP, OWNER_ID, SKU_ID, FROM_LOC, TO_LOC, LOT, LPN_ID, QTY, QTY_MOVED, QTY_INPICK_LOC, PRIORITY, UOM_ID, PACK_ID, ID_MOVED, LOT_MOVED, LOC_MOVED_FROM, REASON, TO_LPN_ID, FINAL_TO_LOC, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@REPLENISHMENT ";
        private string SQL_UPDATE_REPLENISHMENT = " UPDATE @_@REPLENISHMENT SET WH_ID = @WH_ID, OWNER_ID = @OWNER_ID, SKU_ID = @SKU_ID, FROM_LOC = @FROM_LOC, TO_LOC = @TO_LOC, LOT = @LOT, LPN_ID = @LPN_ID, QTY = @QTY, QTY_MOVED = @QTY_MOVED, QTY_INPICK_LOC = @QTY_INPICK_LOC, PRIORITY = @PRIORITY, UOM_ID = @UOM_ID, PACK_ID = @PACK_ID, ID_MOVED = @ID_MOVED, LOT_MOVED = @LOT_MOVED, LOC_MOVED_FROM = @LOC_MOVED_FROM, REASON = @REASON, TO_LPN_ID = @TO_LPN_ID, FINAL_TO_LOC = @FINAL_TO_LOC, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE REPLENISHMENT_ID = @REPLENISHMENT_ID AND REPLENISHMENT_GROUP = @REPLENISHMENT_GROUP  ";
        private string TO_LOC = "@TO_LOC";
        private string TO_LPN_ID = "@TO_LPN_ID";
        private string UOM_ID = "@UOM_ID";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckReplenishmentIDUnique(string replenishmentID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPLENISHMENT_ID", replenishmentID), new SqlParameter("@REPLENISHMENT_GROUP", replenishmentID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_REPLENISHMENT_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckReplenishmentIDUnique(DataBase dataBase, DbTransaction tran, string replenishmentID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPLENISHMENT_ID", replenishmentID), new SqlParameter("@REPLENISHMENT_GROUP", replenishmentID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_REPLENISHMENT_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteReplenishment(List<string> replenishmentID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < replenishmentID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPLENISHMENT_ID", replenishmentID[j]), new SqlParameter("@REPLENISHMENT_GROUP", replenishmentID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_REPLENISHMENT.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteReplenishment(DataBase dataBase, DbTransaction tran, List<string> replenishmentID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < replenishmentID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPLENISHMENT_ID", replenishmentID[j]), new SqlParameter("@REPLENISHMENT_GROUP", replenishmentID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_REPLENISHMENT.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllReplenishment(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_REPLENISHMENT.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllReplenishment(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_REPLENISHMENT.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public ReplenishmentInfo GetReplenishmentByID(string replenishmentID, string replenishmentGroup, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_REPLENISHMENT.Replace("@_@", whLoginID) + " WHERE REPLENISHMENT_ID = @REPLENISHMENT_ID AND REPLENISHMENT_GROUP = @REPLENISHMENT_GROUP  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPLENISHMENT_ID", replenishmentID), new SqlParameter("@REPLENISHMENT_GROUP", replenishmentGroup) };
            ReplenishmentInfo replenishmentInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    replenishmentInfo = new ReplenishmentInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["REPLENISHMENT_ID"].ToString(), reader["REPLENISHMENT_GROUP"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["FROM_LOC"].ToString(), reader["TO_LOC"].ToString(), reader["LOT"].ToString(), reader["LPN_ID"].ToString(), (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, (reader["QTY_MOVED"].ToString() != "") ? decimal.Parse(reader["QTY_MOVED"].ToString()) : 0M, (reader["QTY_INPICK_LOC"].ToString() != "") ? decimal.Parse(reader["QTY_INPICK_LOC"].ToString()) : 0M, reader["PRIORITY"].ToString(), reader["UOM_ID"].ToString(), reader["PACK_ID"].ToString(), reader["ID_MOVED"].ToString(), reader["LOT_MOVED"].ToString(), reader["LOC_MOVED_FROM"].ToString(), reader["REASON"].ToString(), reader["TO_LPN_ID"].ToString(), reader["FINAL_TO_LOC"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return replenishmentInfo;
        }

        public ReplenishmentInfo GetReplenishmentByID(DataBase dataBase, DbTransaction tran, string replenishmentID, string replenishmentGroup, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_REPLENISHMENT.Replace("@_@", whLoginID) + " WHERE REPLENISHMENT_ID = @REPLENISHMENT_ID AND REPLENISHMENT_GROUP = @REPLENISHMENT_GROUP  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@REPLENISHMENT_ID", replenishmentID), new SqlParameter("@REPLENISHMENT_GROUP", replenishmentGroup) };
            ReplenishmentInfo replenishmentInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                replenishmentInfo = new ReplenishmentInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["REPLENISHMENT_ID"].ToString(), reader["REPLENISHMENT_GROUP"].ToString(), reader["OWNER_ID"].ToString(), reader["SKU_ID"].ToString(), reader["FROM_LOC"].ToString(), reader["TO_LOC"].ToString(), reader["LOT"].ToString(), reader["LPN_ID"].ToString(), (reader["QTY"].ToString() != "") ? decimal.Parse(reader["QTY"].ToString()) : 0M, (reader["QTY_MOVED"].ToString() != "") ? decimal.Parse(reader["QTY_MOVED"].ToString()) : 0M, (reader["QTY_INPICK_LOC"].ToString() != "") ? decimal.Parse(reader["QTY_INPICK_LOC"].ToString()) : 0M, reader["PRIORITY"].ToString(), reader["UOM_ID"].ToString(), reader["PACK_ID"].ToString(), reader["ID_MOVED"].ToString(), reader["LOT_MOVED"].ToString(), reader["LOC_MOVED_FROM"].ToString(), reader["REASON"].ToString(), reader["TO_LPN_ID"].ToString(), reader["FINAL_TO_LOC"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return replenishmentInfo;
        }

        public DataSet GetReplenishmentByQueryList(List<string> sqlWhere, ReplenishmentQueryEntity replenishmentQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_REPLENISHMENT.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!replenishmentQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, replenishmentQuery.CurrentPage, replenishmentQuery.PageSize, replenishmentQuery.SortField, replenishmentQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetReplenishmentByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, ReplenishmentQueryEntity replenishmentQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_REPLENISHMENT.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, replenishmentQuery.CurrentPage, replenishmentQuery.PageSize, replenishmentQuery.SortField, replenishmentQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertReplenishment(ReplenishmentInfo replenishmentInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                replenishmentInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Replenishment_Parameters(replenishmentInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_REPLENISHMENT.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertReplenishment(DataBase dataBase, DbTransaction tran, ReplenishmentInfo replenishmentInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                replenishmentInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Replenishment_Parameters(replenishmentInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_REPLENISHMENT.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_Replenishment_Parameters(ReplenishmentInfo replenishmentInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { 
                new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.REPLENISHMENT_ID, SqlDbType.VarChar, 20), new SqlParameter(this.REPLENISHMENT_GROUP, SqlDbType.VarChar, 20), new SqlParameter(this.OWNER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.SKU_ID, SqlDbType.VarChar, 50), new SqlParameter(this.FROM_LOC, SqlDbType.VarChar, 20), new SqlParameter(this.TO_LOC, SqlDbType.VarChar, 20), new SqlParameter(this.LOT, SqlDbType.VarChar, 20), new SqlParameter(this.LPN_ID, SqlDbType.VarChar, 20), new SqlParameter(this.QTY, SqlDbType.Decimal, 13), new SqlParameter(this.QTY_MOVED, SqlDbType.Decimal, 13), new SqlParameter(this.QTY_INPICK_LOC, SqlDbType.Decimal, 13), new SqlParameter(this.PRIORITY, SqlDbType.VarChar, 20), new SqlParameter(this.UOM_ID, SqlDbType.VarChar, 20), new SqlParameter(this.PACK_ID, SqlDbType.VarChar, 50), new SqlParameter(this.ID_MOVED, SqlDbType.VarChar, 20), 
                new SqlParameter(this.LOT_MOVED, SqlDbType.VarChar, 20), new SqlParameter(this.LOC_MOVED_FROM, SqlDbType.VarChar, 20), new SqlParameter(this.REASON, SqlDbType.VarChar, 20), new SqlParameter(this.TO_LPN_ID, SqlDbType.VarChar, 20), new SqlParameter(this.FINAL_TO_LOC, SqlDbType.VarChar, 20), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20)
             };
            if (!string.IsNullOrEmpty(replenishmentInfo.WhID))
            {
                paramArray[0].Value = replenishmentInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = replenishmentInfo.ReplenishmentID;
            paramArray[2].Value = replenishmentInfo.ReplenishmentGroup;
            paramArray[3].Value = replenishmentInfo.OwnerID;
            paramArray[4].Value = replenishmentInfo.SkuID;
            paramArray[5].Value = replenishmentInfo.FromLoc;
            paramArray[6].Value = replenishmentInfo.ToLoc;
            paramArray[7].Value = replenishmentInfo.Lot;
            paramArray[8].Value = replenishmentInfo.LpnID;
            paramArray[9].Value = replenishmentInfo.Qty;
            paramArray[10].Value = replenishmentInfo.QtyMoved;
            paramArray[11].Value = replenishmentInfo.QtyInpickLoc;
            paramArray[12].Value = replenishmentInfo.Priority;
            paramArray[13].Value = replenishmentInfo.UomID;
            paramArray[14].Value = replenishmentInfo.PackID;
            if (!string.IsNullOrEmpty(replenishmentInfo.IdMoved))
            {
                paramArray[15].Value = replenishmentInfo.IdMoved;
            }
            else
            {
                paramArray[15].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(replenishmentInfo.LotMoved))
            {
                paramArray[0x10].Value = replenishmentInfo.LotMoved;
            }
            else
            {
                paramArray[0x10].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(replenishmentInfo.LocMovedFrom))
            {
                paramArray[0x11].Value = replenishmentInfo.LocMovedFrom;
            }
            else
            {
                paramArray[0x11].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(replenishmentInfo.Reason))
            {
                paramArray[0x12].Value = replenishmentInfo.Reason;
            }
            else
            {
                paramArray[0x12].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(replenishmentInfo.ToLpnID))
            {
                paramArray[0x13].Value = replenishmentInfo.ToLpnID;
            }
            else
            {
                paramArray[0x13].Value = DBNull.Value;
            }
            paramArray[20].Value = replenishmentInfo.FinalToLoc;
            paramArray[0x15].Value = replenishmentInfo.CreatedBy;
            paramArray[0x16].Value = replenishmentInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateReplenishment(ReplenishmentInfo replenishmentInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                replenishmentInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Replenishment_Parameters(replenishmentInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_REPLENISHMENT.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateReplenishment(DataBase dataBase, DbTransaction tran, ReplenishmentInfo replenishmentInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                replenishmentInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_Replenishment_Parameters(replenishmentInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_REPLENISHMENT.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

